<template>
  <div class="detail_box" >
    <!-- 操作 -->
    <div class="operate_box">
      <div class="item_box">
        <div class="operate_item">
          <label>用户ID：</label>
          <el-input type="number" placeholder="请输入用户ID" v-model="keyword"></el-input>
        </div>
        <div class="operate_item">
          <label>状态：</label>
          <el-select
            filterable
            v-model="status"
            placeholder="请选择状态">
            <el-option
              v-for="(item,index) in statusOption"
              :key="index"
              :label="item.name"
              :value="item.id">
            </el-option>
          </el-select>
        </div>
        <div class="operate_item">
          <span class="operate_btn search_btn" @click="getTableInfo(1)">查询</span>
          <span class="operate_btn reset_btn" @click="resetTable">重置</span>
        </div>
      </div>
      <div class="btn_box" >
        <span class="operate_btn search_btn" @click="confirmEdit" >解除绑定</span>
      </div>
    </div>
    <!-- 表格 -->
    <div class="table_box">
      <el-table
        border
        height="440"
        v-loading="loading"
        :header-cell-style="tableHeader"
        ref="multipleTable"
        :data="tableInfo"
        tooltip-effect="dark"
        :row-key="getRowKeys"
        @selection-change="selectionChange"
        style="width: 100%;">
        <el-table-column
          type="selection"
          :reserve-selection="true"
          width="55">
        </el-table-column>
        <el-table-column
          width="80"
          prop="id"
          label="ID">
        </el-table-column>
        <el-table-column
          width="160"
          sortable
          prop="modifyTime"
          label="修改时间">
          <template slot-scope="scope">
            <p style="text-align: left">{{formatTime(scope.row.modifyTime)}}</p>
          </template>
        </el-table-column>
        <el-table-column
          prop="wxName"
          label="微信名称">
        </el-table-column>
        <el-table-column
          label="微信头像">
          <template slot-scope="scope">
            <div>
              <img :src="scope.row.wxPhoto" class="icon" v-if="scope.row.wxPhoto">
              <p v-else>——</p>
            </div>
          </template>
        </el-table-column>
        <el-table-column
          label="状态">
          <template slot-scope="scope">
            <div :style="'color:'+(scope.row.enable===1?'#67c430':'#e60000')">
              {{scope.row.enable===1?'可用':'冻结'}}
            </div>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="record_page">
      <span class="page_count">共<i>{{totals}}</i>条</span>
      <el-pagination
        @size-change="sizeChange"
        @current-change="getTableInfo"
        background
        :current-page="currentPage"
        :page-sizes="[15, 20, 30, 40]"
        :page-size="pageSize"
        layout="prev, pager, next, sizes,jumper"
        :total="totals">
      </el-pagination>
    </div>
  </div>
</template>

<script>
import {getBindList, removeChild} from "@/api/data/user";
import commons from "../../../utils/common";

export default {
  props: ['userId'],
  data() {
    return {
      dataLoad: false,
      keyword: '',
      status: '',
      statusOption: [
        {
          id: '',
          name: '全部'
        },
        {
          id: 1,
          name: '正常'
        }, {
          id: 0,
          name: '禁用'
        },
      ],
      tableInfo: [],
      currentPage: 1,
      pageSize: 10,
      totals: 0,
      loading:false,
      selectItem:'',
      selectArr:[]
    }
  },

  mounted() {
    this.getTableInfo(1);
  },

  methods: {
    getTableInfo(page) {
      let that = this;
      that.loading = true;
      that.currentPage = page;
      let queryParams = {
        limit:that.pageSize,
        page:page,
        keyword:that.keyword,
        enable:that.status,
        searchId:that.keyword,
        parentId:that.userId
      }
      getBindList(queryParams).then(res => {
        that.tableInfo = res?res.list:[];
        that.totals = res?res.count:0;
        that.loading = false;
      }).catch((e) => {
        that.loading = false;
      });
    },

    resetTable() {
      this.keyword = '';
      this.status = '';
      this.pageSize = 10;
      this.getTableInfo(1)
    },



    formatIds(arr){
      let that = this;
      if (that.selectArr){
        that.selectArr.forEach((id)=>{
          arr.forEach((item)=> {
            if (item.id===id){
              that.$refs.multipleTable.toggleRowSelection(item);
            }
          })
        })
      }
    },

    // 获取选中列的id
    getRowKeys(row) {
      return row.id;
    },

    // 多选
    selectionChange(val) {
      let that = this;
      let data = '';
      val.forEach((item, i) => {
        data = data + (i > 0 ? ',' : '') + item.id;
      });
      that.selectItem = data;
    },

    confirmEdit() {
      let that = this;
      that.$confirm((that.selectItem===''?'当前未选择下级，确定进行该操作吗？':'确定解除绑定所选下级吗？'), '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        if(this.selectItem === '') {
          return;
        }
        this.bindEvents()
      }).catch(() => {});
    },

    bindEvents() {
      let that = this;
      that.dataLoad = true;
      removeChild({childIds:that.selectItem}).then((res)=>{
          that.getTableInfo(1);
          that.$message.success({
            message:'解绑成功！',
            duration:1500
          });
        that.dataLoad = false;
      }).catch((err)=> {
        that.$message.error({
          message:res.data.message,
          duration:1500
        });
      })
    },

    closeTip(refresh) {
      this.$emit('listenToChild', {refresh: refresh})
    },

    sizeChange(val) {
      this.pageSize = val;
      this.getTableInfo(1);
    },

    formatTime(time) {
      return commons.formatTime(time);
    },

    // 设置table标题居中
    tableHeader({row, column, rowIndex, columnIndex}) {
      return 'background:#F0F2F5;color:#111;padding:10px 0;font-size:14px;font-weight:600'
    },
  }
}
</script>

<style>
.el-dialog__body {
  padding: 0 20px 20px !important;
}
</style>
<style scoped>
.detail_box {
  position: relative;
  width: 100%;
  box-sizing: border-box;
  min-width: 0;
  height: 600px;
  min-height: 600px;
  padding: 0;
}

</style>

